package com.jt.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;


import java.util.List;
@Slf4j//输出日志
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private RemoteUserService remoteUserService;

    @Override//我们执行登录操作时，提交登录按钮系统会调用此方法，不需要controller
    public UserDetails loadUserByUsername(String username)
            throws UsernameNotFoundException {
        com.jt.pojo.User user = remoteUserService.selectUserByUsername(username);
        if (user==null)
            throw new UsernameNotFoundException("用户不存在");
        List<String> permissions = remoteUserService.selectUserPermissions(user.getId());
        log.info(username+" permissions {}",permissions.toString());
        User userInfo = new User(username,
                user.getPassword(),
                AuthorityUtils.createAuthorityList(permissions.toArray(new String[]{})));
        return userInfo;
    }
}
